package com.es.controller;

import com.es.domain.Student;
import com.es.service.StudentServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 描述:
 * es-meta-
 *
 * @author Derek
 * @create 2021-11-29 11:21
 */
@Controller
@RequestMapping("/student")
public class StudentController extends BaseController{

    @Autowired
    private StudentServiceI service;

    @RequestMapping("/manage")
    public String manage(HttpServletRequest request){
        //
        List<Student> list = service.list();
        request.setAttribute("list",list);
        return "student/manage";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return "student/addPage";
    }

    @RequestMapping("/save")
    public String save(Student student){
        int i = service.save(student);
        if(i>0){
            return "forward:manage";
        }else{
            return "fail";
        }
    }
    @RequestMapping("/save1")
    public String save1(@RequestParam("birthday") Date day){
        /*int i = service.save(student);
        if(i>0){
            return "forward:manage";
        }else{
            return "fail";
        }*/
        System.out.println(day);
        return "forward:manager";
    }

    @RequestMapping("/updatePage")
    public String updatePage(Integer id,HttpServletRequest request){
        Student student = service.get(id);
        request.setAttribute("stu",student);
        return "student/updatePage";
    }

    @RequestMapping("/update")
    public String update(Student student){
        int i = service.update(student);
        if(i>0){
            return "forward:manage";
        }else{
            return "fail";
        }
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        int i = service.delete(id);
        if(i>0){
            return "forward:manage";
        }else{
            return "fail";
        }
    }

   /* @InitBinder
    public void initDateBinder(WebDataBinder webDataBinder){
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }*/
}
